﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;

namespace sjzd
{
    /// <summary>
    /// WPFAddQX.xaml 的交互逻辑
    /// </summary>
    public partial class WPFAddQX : Window
    {
        public WPFAddQX()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            string strQXname = QXName.Text, strQXID = QXID.Text;
            string configpathPath = System.Environment.CurrentDirectory + @"\设置文件\路径设置.txt";
            string configXZPath = System.Environment.CurrentDirectory + @"\设置文件\旗县乡镇.txt";
            StreamReader sr = new StreamReader(configXZPath, Encoding.Default);
            String line;
            //读取设置文件的旗县乡镇文件中第一行，确认旗县镇数
            line = sr.ReadLine();
            sr.Close();
            string[] linShi1 = line.Split(':');
            int intQXGS = Convert.ToInt16(linShi1[1]);
            sr = new StreamReader(configXZPath, Encoding.Default);
            while ((line = sr.ReadLine()) != null)
            {            
                if(line.Contains(strQXname)|| line.Contains(strQXID))
                {
                    MessageBox.Show("增加的旗县已经存在，请重新输入","警告",MessageBoxButton.OK);
                    sr.Close();
                    return;
                }
            }
            sr.Close();
            intQXGS++;
            sr = new StreamReader(configXZPath, Encoding.Default);
            string strData = "";
            int intLS = 0;
            while((line=sr.ReadLine())!=null)
            {
                if(intLS==0)
                {
                    string[] LS = line.Split(':');
                    strData = strData + LS[0] + ':' + intQXGS.ToString() + "\r\n";
                }
                else
                {
                    if(intLS==(2*intQXGS-1))
                    {
                        strData+=strQXname + "\r\n"+strQXID + "\r\n";
                    }
                    strData+=line + "\r\n";
                }
                intLS++;

            }
            sr.Close();
            FileStream fs = new FileStream(configXZPath,FileMode.OpenOrCreate);
            StreamWriter sw = new StreamWriter(fs,Encoding.Default);
            sw.Write(strData);
            sw.Flush();
            sw.Close();
            fs.Close();

        }
    }
}
